package com.my.vote.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.my.vote.R;
import com.my.vote.entity.EVoteInfo;
import com.my.vote.entity.EVoteItem;
import com.my.vote.widget.RxTimeButton;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Action;

/**
 * 功能描述:
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/4/12
 */
public class ItemAdapter extends BaseQuickAdapter<EVoteItem, BaseViewHolder> {

    Context context;

    List<EVoteItem> select = new ArrayList<>(5);

    public ItemAdapter(Context context) {
        super(R.layout.voite_item_adapter);
        this.context = context;

    }

    @Override
    protected void convert(BaseViewHolder helper, final EVoteItem item) {

        int posistion = helper.getAdapterPosition();
        helper.setText(R.id.tvSerialNumber, "Number:" + item.getSerialNumber());
        helper.setText(R.id.tvTitle, item.getTitle());
        helper.setText(R.id.tvDesc, item.getIntroduce());
        helper.setText(R.id.tvVoteSize, "VoteSize:" + item.getVoteSize());
        helper.setText(R.id.tvRanking, "Ranking:" + (posistion + 1));


        if (posistion <= 2) {
            helper.setVisible(R.id.ivIndex, true);
            int index = posistion == 0 ? R.mipmap.bfj :
                    posistion == 1 ? R.mipmap.bfk : R.mipmap.bfi;
            helper.setImageResource(R.id.ivIndex, index);
        } else {
            helper.setVisible(R.id.ivIndex, false);
        }

        helper.setChecked(R.id.cbSelect, isCheck(item));
        helper.setOnClickListener(R.id.cbSelect, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contatus(item);
            }
        });
    }


    boolean isCheck(EVoteItem item) {
        for (EVoteItem voteItem : select) {
            if (TextUtils.equals(item.getObjectId(), voteItem.getObjectId())) {
                return true;
            }
        }
        return false;
    }

    void contatus(EVoteItem item) {
        final int size = select.size();
        for (int i = 0; i < size; i++) {
            if (TextUtils.equals(item.getObjectId(), select.get(i).getObjectId())) {
                select.remove(i);
                notifyDataSetChanged();
                return;
            }
        }
        select.add(item);
        notifyDataSetChanged();
    }

    public void clearSelect() {
        select.clear();
        notifyDataSetChanged();
    }

    public List<EVoteItem> getSelect() {
        return select;
    }

}
